package com.example.restatr.Atcivity.Park;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class SetCarInfoAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private EditText type;
    private EditText address;
    private TextView selectMileage;
    private MaterialButton Delete;
    private String title, mold, id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_set_cl_info_at );

        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        type = findViewById( R.id.mold );
        address = findViewById( R.id.address );
        Delete = findViewById( R.id.Delete );
        selectMileage = findViewById(R.id.selectMileage);
        mold = getIntent().getStringExtra( "mold" );
        id = getIntent().getStringExtra( "id" );
        if (mold.equals( "add" )) {
            title = "添加车辆信息";
        } else {
            title = "编辑车辆信息";
            type.setText( getIntent().getStringExtra( "type" ) );
            address.setText( getIntent().getStringExtra( "platNo" ) );
            Delete.setVisibility( View.VISIBLE );
            selectMileage.setVisibility( View.VISIBLE );
        }
        Tools.SetBarTitle( tb, barTitle, title, search, SetCarInfoAt.this,false );
        address.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                selectMileage.setVisibility( View.VISIBLE );
            }
        } );
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Commit:
                if (Tools.isEmptyText(type)&&Tools.isEmptyText(address)){
                    if (mold.equals( "add" )) {
                        new Thread( () -> {
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put( "type", type.getText().toString() );
                                jsonObject.put( "plateNo", address.getText().toString() );
                                String token = Tools.Post( "/prod-api/api/park/car", MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                                JSONObject jx = new JSONObject( token );
                                String msg = jx.getString( "msg" );
                                int code = jx.getInt( "code" );
                                runOnUiThread( () -> {
                                    if (code == 200) {
                                        Toast.makeText( this, "保存成功", Toast.LENGTH_SHORT ).show();
                                        finish();
                                    } else {
                                        Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();
                                    }
                                } );
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } ).start();
                    } else if (mold.equals( "edit" )) {
                        new Thread( () -> {
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put( "id", id );
                                jsonObject.put( "type", type.getText().toString() );
                                jsonObject.put( "plateNo", address.getText().toString() );
                                String token = Tools.Put( "/prod-api/api/park/car", MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                                JSONObject jx = new JSONObject( token );
                                String msg = jx.getString( "msg" );
                                int code = jx.getInt( "code" );
                                runOnUiThread( () -> {
                                    if (code == 200) {
                                        Toast.makeText( this, "保存成功", Toast.LENGTH_SHORT ).show();
                                        finish();
                                    } else {
                                        Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();
                                    }
                                } );
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } ).start();
                    }
                }else {
                    Toast.makeText(this, "不能为空！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.Delete:
                new Thread( () -> {
                    try {
                        String delete = Tools.Delete( "/prod-api/api/park/car/" + getIntent().getStringExtra( "id" ), MainActivity.sp.getString( "token", "" ) );
                        JSONObject all = new JSONObject( delete );
                        int code = all.getInt( "code" );
                        runOnUiThread( () -> {
                            if (code == 200) {
                                Toast.makeText( this, "删除成功！", Toast.LENGTH_SHORT ).show();
                                finish();
                            } else {
                                Toast.makeText( this, "删除失败", Toast.LENGTH_SHORT ).show();
                            }
                        } );
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } ).start();
                break;
            case R.id.selectMileage:
                Intent intent = new Intent( SetCarInfoAt.this, CarMileageAt.class );
                intent.putExtra( "plateNo",address.getText().toString() );
                startActivity( intent );
                break;
        }
    }
}